
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import weka.classifiers.bayes.NaiveBayes;
//import weka.classifiers.rules.Prism;
//import weka.classifiers.trees.J48;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;
import weka.filters.Filter;
import weka.filters.supervised.attribute.Discretize;


public class FormWindowListener implements ActionListener
{
	
	/* ------------------------------------------------------------------ */
	// PROPERTIES
	/* ------------------------------------------------------------------ */
	
	private FormWindow form;
	
	private String datasource;
	public String getDatasource() 
	{
		if (datasource == null)
		{
			return "C:\\Users\\Public\\eclipse_workspace\\TP_WEKA\\data\\credit-g.arff";
			//return ".\\data\\credit-g.arff";
		}
		return datasource;
	}
	
	private JFileChooser chooser;
	public JFileChooser getChooser() {
		return chooser;
	}
	public void setChooser(JFileChooser chooser) {
		this.chooser = chooser;
	}
	
	
	/* ------------------------------------------------------------------ */
	// CONSTRUCTORS
	/* ------------------------------------------------------------------ */
	
	public FormWindowListener(FormWindow fw) 
	{
		this.form = fw;
	}
	
	
	
	/* ------------------------------------------------------------------ */
	// METHODS
	/* ------------------------------------------------------------------ */
		
	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		// Choix du fichier ARFF a selectionner
		if (arg0.getActionCommand().equals("Ouvrir")) 
		{
			String fic = this.getFileChoosed();
			this.datasource = fic;
			//this.datasource = this.getDatasource();
			
			System.out.println("Fichier ARFF selectionne : " + this.getDatasource());

		}
		// Traitement
		else if (arg0.getActionCommand().equals("Valider")) 
		{
			try 
			{
				Instances instances;

				instances = DataSource.read(this.getDatasource());
				
				// construction de l'instance
				Object[] data = new String[7];
				data[0] = this.form.get_jc_checking_status().getSelectedItem().toString();
				data[1] = this.form.get_jc_credit_history().getSelectedItem().toString();
				data[2] = this.form.get_jc_savings_status().getSelectedItem().toString();
				data[3] = this.form.get_jc_housing().getSelectedItem().toString();
				data[4] = this.form.get_jc_other_payment_plans().getSelectedItem().toString();
				data[5] = this.form.get_jc_purpose().getSelectedItem().toString();
				data[6] = this.form.get_jc_foreign_worker().getSelectedItem().toString();

				System.out.println((String)data[0]);
				System.out.println((String)data[1]);
				System.out.println((String)data[2]);
				System.out.println((String)data[3]);
				System.out.println((String)data[4]);
				System.out.println((String)data[5]);
				System.out.println((String)data[6]);
				
				//System.out.println(instances);
				
				Instance instance = this.createInstance(data, instances);
				System.out.println("INSTANCE : ");
				System.out.println("------------------------");
				System.out.println(instance);
				System.out.println("------------------------");
				
				
				// NAIVE BAYESIEN
				NaiveBayes nb = new NaiveBayes();
				instances.setClassIndex(instances.numAttributes() - 1);
				nb.buildClassifier(instances);
				double[] probaBAYESIEN = nb.distributionForInstance(instance);
				double[] summaryNB = new double[3];
				
				//			    
				System.out.println("\n\nNAIVE BAYES");
				System.out.println("-------------------");

				
				System.out.println("Nb instances : " + instances.numInstances());
				
				int numCorrect = 0;
				for (int i = 0; i < instances.numInstances(); i++) 
				{
					Instance currentInst = instances.instance(i);
					double predictedClass = nb.classifyInstance(currentInst);
					if (predictedClass == currentInst.classValue()) 
					{
						numCorrect++;
					}
				}
				System.out.println(numCorrect/10 + " %");

				for (double d : probaBAYESIEN) {
					System.out.print(d + ",");
				}

				summaryNB[0] = numCorrect/10;
				summaryNB[1] = probaBAYESIEN[0];
				summaryNB[2] = probaBAYESIEN[1];
				
				
				
				new ResultWindow(summaryNB[1]* 100);
				
				// END
				System.out.println("\n-------------------");
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
	}

	private String getFileChoosed() {
		chooser = new JFileChooser("./");
		FileNameExtensionFilter filter = new FileNameExtensionFilter(
				"fichiers arff", "arff");
		chooser.setFileFilter(filter);
		int returnVal = chooser.showOpenDialog(form);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			return chooser.getSelectedFile().getPath();
		} else {
			return null;
		}
	}

	public static Instance createInstance(Object[] data, Instances instances) 
	{
		Instance instance = new Instance(20);		
		instance.setDataset(instances);
		
			instance.setValue(instances.attribute("checking_status"), (String) data[0]);
			instance.setValue(instances.attribute("credit_history"), (String) data[1]);
			instance.setValue(instances.attribute("savings_status"), (String) data[2]);
			instance.setValue(instances.attribute("housing"), (String) data[3]);
			instance.setValue(instances.attribute("other_payment_plans"), (String) data[4]);
			instance.setValue(instances.attribute("purpose"), (String) data[5]);
			instance.setValue(instances.attribute("foreign_worker"), (String) data[6]);
		
		return instance;
	}
}
